<template>
  <div class="tab-bar-item" @click="itemClick">
    <slot name="item-icon" v-if="!isActive"></slot>
    <slot name="item-icon-active" v-else></slot>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'tabbar-item',
  props: {
    path: String,
    activeColor: {
      type: String,
      default: '#ff921f'
    }
  },
  data() {
    return {

    }
  },
  computed: {
    isActive() {
      return this.$route.path == this.path;
    },
    activeStyle() {
      return this.isActive ? {color: this.activeColor} : {}
    }
  },
  methods: {
    itemClick() {
      if(this.$route.path === this.path) return;
      this.$router.replace(this.path);
    }
  }
}
</script>

<style>
/* 该样式存在.tab-bar-item img应用与子元素img上，故不使用scoped */
  .tab-bar-item {
    height: 49px;
    font-size: 14px;
    text-align: center;
    flex: auto;
  }

  .tab-bar-item img {
    width: 22px;
    height: 22px;
    margin: 3px auto 2px;
    vertical-align: middle;
  }
</style>